﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Area
{
    public class SearchAreaModel
    {
        [DisplayName("Buscar por Nombre")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        public List<SearchAreaInfo> AreaInfo
        {
            get
            {
                return areaInfo;
            }
        }

        private List<SearchAreaInfo> areaInfo;

        public SearchAreaModel()
        {
            SearchArea();
        }

        public void SearchArea(String pattern)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var managements = from a in entities.areas
                             where a.name.Contains(pattern) && a.enabled
                             select new {
                                 AreaId = a.areaId,
                                 Name = a.name,                                  
                                 Process = a.process.name};
            areaInfo = new List<SearchAreaInfo>();

            foreach (var man in managements)
            {
                areaInfo.Add(
                    new SearchAreaInfo
                    {
                        AreaId = man.AreaId,
                        Name = man.Name,                        
                        Process = man.Process
                    });
            }  
            
        }

        public void SearchArea()
        {
            SearchArea("");
        }
    }
}